<?php
error_reporting(0);
$nom = $_POST["arguments"];
switch ($_POST["functionname"]) {

    case 'getda':
        geta();
        break;
    case 'chj':
        chj($nom[0]);
        break;
    case 'snd':
        snd($nom[0]);
        break;
    case 'getaa':
        getaa($nom[0], $nom[1]);
        break;
    case 'gets':
        gets($nom[0], $nom[1]);
        break;
    case 'ref':
        draw();
        break;
    case 'cb':
        cb($nom[0], $nom[1]);
        break;
    case 'bdmov':
        bdmov($nom[0], $nom[1], $nom[2]);
        break;
    case 'hb':
        hb($nom[0], $nom[1], $nom[2]);
        break;
    case 'bdmovr':
        bdmovr($nom[0], $nom[1], $nom[2]);
        break;
    case 'cf':
        cf($nom[0], $nom[1], $nom[2]);
        break;
}
function snd($m){
$json_string = file_get_contents('usr.json');

    $d = json_decode($json_string,true);
    ob_start(); 
    $userName=$_COOKIE['usn'];
    $password=$_COOKIE['pwd'];
ob_end_flush();
    if($d[$userName]['pass']===$password&&$userName!=null){
        $d['msg']=$d['msg']+"\n"+$map["country"][$_SESSION['p']]+":"+$m;$json_strings = json_encode($d);
        file_put_contents("usr.json",$json_strings);
        
       echo "发送成功".$m;
    }else{

        echo "<script type='text/javascript'>";
        echo "window.location.href='login.html'";
        echo "</script>";

    }
}
function mm(){
	$map = $_SESSION['w'];
    $pl = $_SESSION['p'];
$json_string = file_get_contents('usr.json');

    $d = json_decode($json_string,true);
    ob_start(); 
    $userName=$_COOKIE['usn'];
    $password=$_COOKIE['pwd'];
ob_end_flush();
    if($d[$userName]['pass']===$password&&$userName!=null){
        
    }else{

        echo "<script type='text/javascript'>";
        echo "window.location.href='login.html'";
        echo "</script>";

    }
	echo "document.getElementById('mm').innerHTML='金币:" . $map["money"][$pl] . "元<br/>您是".$map["country"][$pl]."<br/>轮到".$map["country"][$map[turn]]."操作';";
   
}
function chj($remark){
$json_string = file_get_contents('usr.json');

    $d = json_decode($json_string,true);
    ob_start(); 
    $userName=$_COOKIE['usn'];
    $password=$_COOKIE['pwd'];
ob_end_flush();
    if($d[$userName]['pass']===$password&&$userName!=null){
        
    }else{

        echo "<script type='text/javascript'>";
        echo "window.location.href='login.html'";
        echo "</script>";

    }
	$json_string = file_get_contents('usr.json');

	$d = json_decode($json_string,true);
	$d[$userName]['remark']="$remark";
    echo "已经将简介更改为".$remark;
    $json_strings = json_encode($d);
    file_put_contents("usr.json",$json_strings);
 
}
function cb($ii,$jj)
{

    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    $k = "";
    $i = 0;
    $u=0;
    foreach ($map["army"][$pl] as $sss) {
  $u++;
    
    }
    $u++;
    $map["army"][$pl][$u]="";
    $map["army"][$pl][$u][x]=$map["cap"][$pl][0];
    $map["army"][$pl][$u][y]=$map["cap"][$pl][1];
    $map["army"][$pl][$u][pep]=$ii;
    $map["army"][$pl][$u][nm]=$jj;
    $map["money"][$pl]-=$map["army"][$pl][$u][pep]*100;
    if($map["money"][$pl]<0){
    	echo "部队数量过多，金币不足";
    	return;
    }
    $map[turn]++;
    $_SESSION['w']=$map;
    echo "购买成功，您可以在首都找到您购买的部队".$jj;
}
function cf($ii,$jj,$nm)
{

    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    $k = "";
    $i = 0;
    $u=0;
    foreach ($map["army"][$pl] as $sss) {
  $u++;
    
    }
    $u++;
    $map["army"][$pl][$u]=$map["army"][$pl][$ii];
    
    $map["army"][$pl][$u][pep]=$nm;
    $map["army"][$pl][$ii][pep]-=$nm;
    if($map["army"][$pl][$ii][pep]<0){
    	echo "部队数量过少，拆分失败";
    	return;
    }
    $map["army"][$pl][$u][nm]=$jj;
    $_SESSION['w']=$map;
    echo "拆分成功";
}
function geta()
{
    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    echo "hei=".$map[he].";wid=".$map[wi].";me=".$pl.";";
    mm();
}

function draw()
{

    $color = array(
        "#BEBEBE",
        "#FF60AF",
        "#FF77FF",
        "#CA8EFF",
        "#9393FF",
        "#84C1FF",
        "#80FFFF",
        "#93FF93",
        "#FFFF6F",
        "#FFBB77",
        "#CF9E9E",
        "#95CACA",
        "#CA8EC2"
    );
    session_start();
    $map = $_SESSION['w'];

    $pl = $_SESSION['p'];
    
         for ($i = 1; $i <= $map[he]; $i++) {
        for ($j = 1; $j <= $map[wi]; $j++) {
        $ss="";
        if($i==$map[cap][$map[$i][$j][own]][0]&&$j==$map[cap][$map[$i][$j][own]][1])$ss="的首都";
            echo "<div id='" . $i . "t" . $j . "' onMouseMove=\"pbm(" . $i . "," . $j . "," . $map[$i][$j][own] . ");cli('a" . $i . "j" . $j . "')\" onclick=\"cc(" . $i . "," . $j . "," . $map[$i][$j][own] . ");pbm(" . $i . "," . $j . "," . $map[$i][$j][own] . ");cli('a" . $i . "j" . $j . "');\" style='position:fixed;width:50px;height:50px;left:" . ($j * 50) . "px;top:" . ($i * 50) . "px;opacity:100%;background-color:" . $color[$map[$i][$j][own]] . "'></div>";
            echo "<div id='a" . $i . "j" . $j . "' onMouseMove='mov(" . $i . "," . $j . "," . $map[$i][$j][own] . ")' style='visibility:hidden;z-index:2;position:fixed;left:" . ($j * 50) . "px;top:" . ($i * 50 + 50) . "px;opacity:70%;background-color:white'>点位(" . $i . "," . $j . ")<br/>所有者:" . $map["country"][$map[$i][$j][own]] . "$ss<br/>地形:" . $map["dx"][$map[$i][$j][bey]] . "</div>";
        }
    }
//管理部队
}
function getaa($ii,$j)
{

$json_string = file_get_contents('usr.json');

    $d = json_decode($json_string,true);
    ob_start(); 
    $userName=$_COOKIE['usn'];
    $password=$_COOKIE['pwd'];
ob_end_flush();
    if($d[$userName]['pass']===$password&&$userName!=null){
        
    }else{

        echo "<script type='text/javascript'>";
        echo "window.location.href='login.html'";
        echo "</script>";

    }
    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    $k = "";
    $i = 0;
    mm();
    echo "document.getElementById('bdddd').innerHTML='';";
    foreach ($map["army"][$pl] as $sss) {
  $i++;
    if(abs($sss[x]-$ii)+abs($sss[y]-$j)<2){
        
        $k = "<tr><td onMouseMove=\\\"cug(\\'" . $sss[x] . "t" . $sss[y] . "\\');\\\"  onMouseDown=\\\"cug(\\'" . $sss[x] . "t" . $sss[y] . "\\');dd[\\'" . $sss[x] . "t" . $sss[y] . "\\']=0;\\\" >" . $sss[nm] . "</td><td>兵力：" . $sss[pep] . "</td><td><button onclick='bdmm=$i;cc(" . $ii.",".$j.",-2);'>使用此部队攻击</button></td></tr>";
        echo "document.getElementById('bdddd').innerHTML=document.getElementById('bdddd').innerHTML+\"$k\";";
        }
    }
}
function hb($ii,$j,$nm)
{

    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    $k = "";
    $i = 0;
    $u=0;
    foreach ($map["army"][$pl] as $sss) {
  $u++;
    if(($sss[x]==$ii&&$sss[y]==$j)){
    
        $i+=$sss[pep];
        $map["army"][$pl][$u][pep]=0;
        $map["army"][$pl][$u][x]=-1;
        
        }
    }
    $u++;
    $map["army"][$pl][$u][pep]=$i;
    
    $map["army"][$pl][$u][x]=$ii;
    $map["army"][$pl][$u][y]=$j;
    $map["army"][$pl][$u][nm]=$nm;
    $_SESSION['w']=$map;
    echo "合并成功";
}
function gets($ii,$j)
{

    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    $k = "";
    $i = 0;
    mm();
    echo "document.getElementById('bdd').innerHTML='';";
    foreach ($map["army"][$pl] as $sss) {
  $i++;
    if(($sss[x]==$ii&&$sss[y]==$j)||$ii===-1&&$sss[x]!=-1){
        
        $k = "<tr><td onMouseMove=\\\"cug(\\'" . $sss[x] . "t" . $sss[y] . "\\');\\\"  onMouseDown=\\\"cug(\\'" . $sss[x] . "t" . $sss[y] . "\\');dd[\\'" . $sss[x] . "t" . $sss[y] . "\\']=0;\\\" >" . $sss[nm] . "</td><td>兵力：" . $sss[pep] . "</td><td><button onclick='movv(" . $i . ")'>移动</button><button onclick='cf(" . $i . ",\\\"".$sss[nm]."\\\")'>拆分</button><button onclick='hb(" . $sss[x] . "," . $sss[y] . ")'>合并</button></td></tr>";
        echo "document.getElementById('bdd').innerHTML=document.getElementById('bdd').innerHTML+\"$k\";";
        }
    }
}
function bdmovr($xx, $yy, $i)
{
    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    if(fd(true)){
    return;
    }
    ld();
    if($pld[$pn]<3){
        buyarmy();
    }
    $dis = (abs($xx - $map["army"][$pl][$i][x]) + abs($yy - $map["army"][$pl][$i][y])) * $map["army"][$pl][$i][pep];
    if ($map[$xx][$yy][own] != $pl) {
        if (abs($xx - $map["army"][$pl][$i][x]) > 1 || abs($yy - $map["army"][$pl][$i][y]) > 1) {
            echo "e3";
            return;
        }
        $pee=0;
        foreach ($map["army"][$map[$xx][$yy][own]] as $sss) {
            if ($sss[x] == $xx && $sss[y] == $yy) {
                $pee+=$sss[pep];
            }
        }
        if (($pee) * 1.5 > $map["army"][$pl][$i][pep]) {
            $j=ceil($map["army"][$pl][$i][pep] /3);
            $map["army"][$pl][$i][pep] = 0;
            $map["money"][$pl] -= $dis;
            echo "您对" . $map["dx"][$map[$xx][$yy][bey]] ."的攻击被敌军瓦解，您的" . $map["army"][$pl][$i][nm] . "全军覆没！消灭了敌军".$j."名士兵";
            foreach ($map["army"][$map[$xx][$yy][own]] as $sss) {
                if ($sss[x] == $xx && $sss[y] == $yy) {
                    if($sss[pep]-$j<0){
                        $j-=$sss[pep];
                        $sss[pep]=0;
                    }else $sss[pep]-=$j;
                }
            }
            $map[turn]++;
            $_SESSION['w'] = $map;
            return;
        } else {
            $map["army"][$pl][$i][pep] -= $pee;
            $map["army"][$pl][$i][x] = $xx;
            $map["army"][$pl][$i][y] = $yy;
            $map[$xx][$yy][own] = $pl;
            $map["money"][$pl] -= $dis;
            $_SESSION['w'] = $map;
$map[turn]++;
            echo "您的" . $map["army"][$pl][$i][nm] . "成功攻下" . $map["dx"][$map[$xx][$yy][bey]] . "！损失了" . $pee . "名士兵。";
            return;
        }
        return;
    } else echo "花费了" . $dis . "金币移动了" . $map["army"][$pl][$i][nm];
    $map[turn]++;
    $map["army"][$pl][$i][x] = $xx;
    $map["army"][$pl][$i][y] = $yy;
    $map["army"][$pl][$i][y] = $yy;
    $map["money"][$pl] -= $dis;
    $_SESSION['w'] = $map;
}
$pld=null;
function ld(){
    session_start();
    $map = $_SESSION['w'];
    for($i=1;$i<=$map[he];$i++){
        for($j=1;$j<=$map[wi];$j++){
            $pld[$map[$i][$j][own]]++;
        }
    }
}
function fd($i){
$map = $_SESSION['w'];
$pl = $_SESSION['p'];
	$turn=$map[turn];
    if ($pl!=$turn) {
        echo "还没轮到您操作!";
        return true;
    }
    
    return false;
}
function buyarmy($id,$num){

    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    if(fd()){
    return;
    }
}
function ai($pn){
    session_start();
    $map = $_SESSION['w'];
    while($map["money"][$pn]>500){
        $fool=false;
        if(rand(1,10)>7){
            $fool=true;
        }
    }
    if($pld[$pn]<3){

    }
}
function bdmov($xx, $yy, $i)
{
    session_start();
    $map = $_SESSION['w'];
    $pl = $_SESSION['p'];
    if(fd(false)){
    return;
    }
    ld();
    if($pld[$pn]<3){
        buyarmy();
    }
    if ((abs($xx - $map["army"][$pl][$i][x]) + abs($yy - $map["army"][$pl][$i][y])) > 4) {
        echo "e1";
        return;
    }
    $dis = (abs($xx - $map["army"][$pl][$i][x]) + abs($yy - $map["army"][$pl][$i][y])) * $map["army"][$pl][$i][pep];
    if ($map[$xx][$yy][own] != $pl) {
        if (abs($xx - $map["army"][$pl][$i][x]) > 1 || abs($yy - $map["army"][$pl][$i][y]) > 1) {
            echo "e3";
            return;
        }
        echo "您要花费" . $dis . "金币用" . $map["army"][$pl][$i][nm] . "攻击" . $map["dx"][$map[$xx][$yy][bey]] . "($xx,$yy)吗？";
        return;
    }
    if ($dis > $map["money"][$pl]) {
        echo "e2";
        return;
    }
    echo "您要花费" . $dis . "金币移动" . $map["army"][$pl][$i][nm] . "吗？";
}
